package com.example.Android_test.activity;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.RadioButton;
import com.example.Android_test.R;
import com.example.Android_test.activity.training.*;
import com.example.Android_test.helpers.SOWEngine;
import com.example.Android_test.helpers.SettingsHelper;
import com.example.Android_test.specs.SetOfWordsSpec;
import com.example.Android_test.util.TrainingUtils;

/**
 * Created with IntelliJ IDEA.
 * User: dns
 * Date: 01.05.13
 * Time: 13:48
 * To change this template use File | Settings | File Templates.
 */
public class SelectTraining extends Activity {
    public static final String TITLE = "SelectSetOfWords";

    private RadioButton radioButtonETR;
    private RadioButton radioButtonTW;
    private RadioButton radioButtonGD;
    private RadioButton radioButtonPUWIC;
    private RadioButton radioButtonSPRINT;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        this.setContentView(R.layout.main_select_training);
        this.setTitle("SelectTraining");

        Button buttonStartSelectedTraining = (Button) findViewById(R.id.st_buttonStartSelectedTraining);
        buttonStartSelectedTraining.setOnClickListener(new ButtonStartSelectedTrainingOnClickListener());

        radioButtonETR = (RadioButton) findViewById(R.id.st_radioButtonETR);
        radioButtonTW = (RadioButton) findViewById(R.id.st_radioButtonTW);
        radioButtonGD = (RadioButton) findViewById(R.id.st_radioButtonGD);
        radioButtonPUWIC = (RadioButton) findViewById(R.id.st_radioButtonPUWIC);
        radioButtonSPRINT = (RadioButton) findViewById(R.id.st_radioButtonSPRINT);

        radioButtonETR.setOnClickListener(new RadioButtonSelectTrainingClickListener());
        radioButtonTW.setOnClickListener(new RadioButtonSelectTrainingClickListener());
        radioButtonGD.setOnClickListener(new RadioButtonSelectTrainingClickListener());
        radioButtonPUWIC.setOnClickListener(new RadioButtonSelectTrainingClickListener());
        radioButtonSPRINT.setOnClickListener(new RadioButtonSelectTrainingClickListener());

        reloadWordSets();
    }

    void reloadWordSets() {
        radioButtonETR.setEnabled(false);
        radioButtonTW.setEnabled(false);
        radioButtonGD.setEnabled(false);
        radioButtonPUWIC.setEnabled(false);
        radioButtonSPRINT.setEnabled(false);

        SetOfWordsSpec setOfWords;
        try {
            setOfWords = SOWEngine.INSTANCE.getSelectedSetOfWord();
        } catch (Exception e) {
            Log.w(Main.LOG_TAG, "SelectTraining.reloadWordSets ERROR " + e.getMessage());
            return;
        }

        if (setOfWords.isTrainingAvailable(TrainingUtils.ID_TRAINING_ETR)) {
            radioButtonETR.setEnabled(true);
            Log.d(Main.LOG_TAG, "TRAINING_ETR available");
        }
        if (setOfWords.isTrainingAvailable(TrainingUtils.ID_TRAINING_TW)) {
            radioButtonTW.setEnabled(true);
            Log.d(Main.LOG_TAG, "TRAINING_TW available");
        }
        if (setOfWords.isTrainingAvailable(TrainingUtils.ID_TRAINING_GD)) {
            radioButtonGD.setEnabled(true);
            Log.d(Main.LOG_TAG, "TRAINING_GD available");
        }
        if (setOfWords.isTrainingAvailable(TrainingUtils.ID_TRAINING_PUWIC)) {
            radioButtonPUWIC.setEnabled(true);
            Log.d(Main.LOG_TAG, "TRAINING_PUWIC available");
        }
        if (setOfWords.isTrainingAvailable(TrainingUtils.ID_TRAINING_SPRINT)) {
            radioButtonSPRINT.setEnabled(true);
            Log.d(Main.LOG_TAG, "TRAINING_SPRINT available");
        }


        String currentTraining = SettingsHelper.INSTANCE.getStringPreference(SettingsHelper.KEY_CURRENT_TRAINING_ID);
        if (currentTraining.equals(TrainingUtils.ID_TRAINING_ETR)) {
            radioButtonETR.performClick();
        }
        if (currentTraining.equals(TrainingUtils.ID_TRAINING_GD)) {
            radioButtonGD.performClick();
        }
        if (currentTraining.equals(TrainingUtils.ID_TRAINING_PUWIC)) {
            radioButtonPUWIC.performClick();
        }
        if (currentTraining.equals(TrainingUtils.ID_TRAINING_SPRINT)) {
            radioButtonSPRINT.performClick();
        }
        if (currentTraining.equals(TrainingUtils.ID_TRAINING_TW)) {
            radioButtonTW.performClick();
        }
    }

    private class RadioButtonSelectTrainingClickListener implements View.OnClickListener {
        @Override
        public void onClick(View v) {
            radioButtonETR.setChecked(false);
            radioButtonTW.setChecked(false);
            radioButtonGD.setChecked(false);
            radioButtonPUWIC.setChecked(false);
            radioButtonSPRINT.setChecked(false);

            ((RadioButton) v).setChecked(true);

            switch (v.getId()) {
                case R.id.st_radioButtonETR:
                    SettingsHelper.INSTANCE.setStringPreference(SettingsHelper.KEY_CURRENT_TRAINING_ID, TrainingUtils.ID_TRAINING_ETR);
                    break;
                case R.id.st_radioButtonGD:
                    SettingsHelper.INSTANCE.setStringPreference(SettingsHelper.KEY_CURRENT_TRAINING_ID, TrainingUtils.ID_TRAINING_GD);
                    break;
                case R.id.st_radioButtonPUWIC:
                    SettingsHelper.INSTANCE.setStringPreference(SettingsHelper.KEY_CURRENT_TRAINING_ID, TrainingUtils.ID_TRAINING_PUWIC);
                    break;
                case R.id.st_radioButtonSPRINT:
                    SettingsHelper.INSTANCE.setStringPreference(SettingsHelper.KEY_CURRENT_TRAINING_ID, TrainingUtils.ID_TRAINING_SPRINT);
                    break;
                case R.id.st_radioButtonTW:
                    SettingsHelper.INSTANCE.setStringPreference(SettingsHelper.KEY_CURRENT_TRAINING_ID, TrainingUtils.ID_TRAINING_TW);
                    break;
            }
        }
    }

    private class ButtonStartSelectedTrainingOnClickListener implements View.OnClickListener {
        @Override
        public void onClick(View v) {
            // TODO code duplication from Main :(
            Intent intent = null;

            String currentTraining = SettingsHelper.INSTANCE.getStringPreference(SettingsHelper.KEY_CURRENT_TRAINING_ID);
            if (currentTraining.equals(TrainingUtils.ID_TRAINING_ETR)) {
                intent = new Intent(Main.getAppContext(), Training_ENG_to_RUS.class);
            }
            if (currentTraining.equals(TrainingUtils.ID_TRAINING_GD)) {
                intent = new Intent(Main.getAppContext(), Training_Guess_Description.class);
            }
            if (currentTraining.equals(TrainingUtils.ID_TRAINING_PUWIC)) {
                intent = new Intent(Main.getAppContext(), Training_Pick_up_word_in_context.class);
            }
            if (currentTraining.equals(TrainingUtils.ID_TRAINING_SPRINT)) {
                intent = new Intent(Main.getAppContext(), Training_Sprint.class);
            }
            if (currentTraining.equals(TrainingUtils.ID_TRAINING_TW)) {
                intent = new Intent(Main.getAppContext(), Training_TypeWriting.class);
            }

            if (intent == null) {
                Log.e(Main.LOG_TAG, "Training name error");
                return;
            }

            startActivity(intent);
        }
    }
}